﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
	// 单例模式
	private static GameController m_Instance = null;
	public static GameController Instance {
		get {
			if(m_Instance == null) {
				m_Instance = GameObject.FindObjectOfType(typeof(GameController)) as GameController;
			}

			if (!m_Instance) {
				GameObject obj = new GameObject("GameController");
				m_Instance = obj.AddComponent<GameController>();
			}
			return m_Instance;
		}
	}

	// 游戏场景的宽度
	public float SceneWidth;
	// 游戏场景的高度
	public float SceneHeight;
	// 一个周期的长度
	public float Interval;
	// 组织细胞的数量
	public int NumberOfTissueCells;
	// 组织细胞团的范围
	public Vector2 RangeOfCellClusters;
	// 病原体的数量
	public int NumberOfPathogens;
	// // 病原体的最大数量
	// public int MaxNumberOfPathogens;
	// 初始能量值
	public int InitialEnergy;
	// 能量值恢复的范围
	public Vector2 RangeOfEnergyRecovery;

	[HideInInspector]
	public int CurrentInterval {
		get; private set;
	}

	[HideInInspector]
	public int CurrentNumberOfPathogens {
		get; private set;
	}

	[HideInInspector]
	public int CurrentNumberOfTissueCell{
		get; private set;
	}

	[HideInInspector]
	public int CurrentEnergy;



	private Coroutine m_TickCoroutine;
	private UnityEvent m_TickEvents = new UnityEvent();

	// 进行游戏的初始化
	private void Awake() {
		CurrentInterval = 0;
		CurrentEnergy = InitialEnergy;

		// 开始计时
		m_TickCoroutine = StartCoroutine(Tick());
	}

	private void Start() {
		GameBegin();
	}
	private void Update() {
		CurrentNumberOfPathogens = GameObject.FindGameObjectsWithTag("Virus").Length;
		CurrentNumberOfTissueCell = GameObject.FindGameObjectsWithTag("TissueCell").Length;
		if(CurrentNumberOfTissueCell == 0) {
			GameOver(false);
		}
		
		if(CurrentNumberOfPathogens == 0) {
			GameOver(true);
		}
	}

#region 外部调用接口
	// 添加事件
	public void AddTickEvent(UnityAction action) {
		m_TickEvents.AddListener(action);
	}

	// 移除事件
	public void RemoveTickEvent(UnityAction action) {
		m_TickEvents.RemoveListener(action);
	}

	public bool ConsumeEnergy(int energy) {
		int balance = CurrentEnergy - energy;

		if(balance < 0) {
			return false;
		} else {
			CurrentEnergy = balance;
			return true;
		}
	}
#endregion

#region 游戏状态
	// 游戏开始
	private void GameBegin() {
		// 随机生成组织细胞
		CellController.Instance.generateOrganizations(NumberOfTissueCells, (int)RangeOfCellClusters.x, (int)RangeOfCellClusters.y);
		// 随机生成病毒
		CellController.Instance.generateVirus(NumberOfPathogens);
		// 随机感染组织细胞
		CellController.Instance.infectCells(3);
		// 随机生成免疫细胞
		CellController.Instance.generateCells(3,4,5);
	}

	// 计时函数
	private IEnumerator Tick() {
		while(true) {
			yield return new WaitForSeconds(Interval);

			CurrentInterval ++;
			
			// 恢复能量
			int energy = Mathf.RoundToInt(Random.Range(RangeOfEnergyRecovery.x, RangeOfEnergyRecovery.y));
			CurrentEnergy += energy;

			// 触发计时事件
			m_TickEvents.Invoke();
		}
	}

	private void GameOver(bool win) {
		// 停止计时
		StopCoroutine(m_TickCoroutine);
		// 禁止放置
		CellPlacementController.Instance.DisablePlacement();

		if(win) {
           GameUIController.Instance.winText();
            GameUIController.Instance.activeOverButton();
            Debug.Log("win");
		} else {
			Debug.Log("lose");
            GameUIController.Instance.lostText();
            GameUIController.Instance.activeOverButton();
        }
	}
    public void ReLoadGame()
    {

        SceneManager.LoadScene(2);
    }
    public void ReLoadMenu()
    {

        SceneManager.LoadScene(1);
    }
    #endregion
}
